﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

#if !WINRT
using System.Windows;
using System.Windows.Media;
#else
using Windows.UI.Xaml;
using Windows.UI.Xaml.Media;
#endif

namespace MishraReader
{
    /// <summary>
    ///   Static helper class to walk up the tree. From Josh Smith's article: http://www.codeproject.com/KB/WPF/WpfElementTrees.aspx
    /// </summary>
    internal static class LogicalTreeWalker
    {
        /// <summary>
        ///   Walks up the logical tree starting at 'initial' and returns the first element of the type T enountered.
        /// </summary>
        /// <param name="initial"> It is assumed that this element is in a logical tree. </param>
        public static T FindParentOfType<T>(DependencyObject initial) where T : class
        {
            var current = FindClosestLogicalAncestor(initial);

            while (current != null && !(current is T))
            {
                current = LogicalTreeHelper.GetParent(current);
            }

            return (current as T);
        }

        /// <summary>
        ///   This method is necessary in case the element is not part of a logical tree. It finds the closest ancestor element which is in a logical tree.
        /// </summary>
        /// <param name="initial"> The element on which the user clicked. </param>
        private static DependencyObject FindClosestLogicalAncestor(DependencyObject initial)
        {
            var current = initial;
            var result = initial;

            while (current != null)
            {
                var logicalParent = LogicalTreeHelper.GetParent(current);
                if (logicalParent != null)
                {
                    result = current;
                    break;
                }
                current = VisualTreeHelper.GetParent(current);
            }

            return result;
        }
    }
}